package com.etc.controller;

import com.etc.pojo.User;
import com.etc.service.UserService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 杰哥
 * @Title: UserController
 * @ProjectName myssm
 * @Description: TODO
 * @date 2019/7/3116:59
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping(value = "select",method = RequestMethod.GET)
    public ModelAndView login(){
        List<User> userList = userService.select();
        ModelAndView mv = new ModelAndView("Userindex");
        mv.addObject("userList",userList);
        return mv;
    }
    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public void delete(@RequestParam (value = "uid") int uid,PrintWriter out){
        Integer delete = userService.deleteById(uid);
        if(delete == 1){
            out.print(1);
        }else{
            out.print(0);
        }
    }
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    public void insert(@RequestParam (value = "username")String username,
                          @RequestParam (value = "password")String password,
                          PrintWriter out){
        Integer insert = userService.insert(new User(username,password));
        if(insert == 1){
            out.print(1);
        }else{
            out.print(0);
        }
    }
    @RequestMapping(value = "findById",method = RequestMethod.GET)
    public void findById(@RequestParam (value = "uid") int uid,PrintWriter out){
        User user = userService.findById(uid);
        Gson gson = new Gson();
        String s = gson.toJson(user);

        if(user != null){
        out.print(s);
        }
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public void insert(@RequestParam (value = "uid") int uid,
                       @RequestParam (value = "username")String username,
                       @RequestParam (value = "password")String password,
                       PrintWriter out){
        Integer insert = userService.update(new User(uid,username,password));
        if(insert == 1){
            out.print(1);
        }else{
            out.print(0);
        }
    }

    @RequestMapping(value = "findByName",method = RequestMethod.GET)
    public void findByName(@RequestParam (value = "username")String username,
                           @RequestParam (value = "password")String password,
                           HttpServletRequest request,PrintWriter out){

        User user = userService.selectName(username);

        if (user == null) {
            out.print(2);
        }else {
            HttpSession session = request.getSession();

            session.setAttribute("user", user);
            String password1 = user.getPassword();
            if(password.equals(password1)){
                out.print(1);
            }

        }
    }
}

